<template>
	<div class="add-exam">
		<eagle-input
			label="选择试卷"
			required
			readonly
			inputalign="right"
			islink
			@click="chooseExam"
		>
			<template #input>
				<span style="color: #999">{{
					data.PaperName || '请选择使用的试卷'
				}}</span>
			</template>
		</eagle-input>
		<eagle-input
			required
			readonly
			inputalign="right"
			label="阅卷人"
			islink
			v-if="data.PaperName"
			@click="goExamPeople('MarkChnName')"
		>
			<template #input>
				<span style="color: #999">{{
					data.MarkChnName || '请选择批阅人'
				}}</span>
			</template>
		</eagle-input>
		<eagle-date
			label="开始时间"
			required
			input-align="right"
			:date.sync="data.ExamStartTime"
		></eagle-date>
		<eagle-date
			label="截止时间"
			required
			input-align="right"
			:date.sync="data.ExamEndTime"
		></eagle-date>
		<eagle-input label="考试时间（分钟）" required readonly inputalign="right">
			<template #input>
				<input class="input-num" v-model="data.ExamMins" type="number" />
			</template>
		</eagle-input>
		<eagle-input
			readonly
			label="允许重复考试"
		>
			<template #input>
				<eagle-checkbox v-model="data.IsAllowedRepeatExam" shape="square" />
			</template>
		</eagle-input>
		<eagle-input
			required
			readonly
			inputalign="right"
			label="最多重考次数"
			v-if="data.IsAllowedRepeatExam"
		>
			<template #input>
				<input class="input-num" v-model="data.RepeatExamTimes" type="number" />
			</template>
		</eagle-input>
		<eagle-input label="评估方式" required readonly>
			<template #input>
				<eagle-radio-group v-model="data.EvaluationType" direction="horizontal">
					<eagle-radio name="1">按分数</eagle-radio>
					<eagle-radio name="2">按比例</eagle-radio>
				</eagle-radio-group>
			</template>
		</eagle-input>
		<div class="bottom-desc" v-if="data.EvaluationType == 1">
			<span>超过</span
			><input class="input-num" v-model="data.EvaluationValue" type="number" />
			<div>分通过，按总分统计评估结果</div>
		</div>
		<div class="bottom-desc" v-if="data.EvaluationType == 2">
			<span>超过</span
			><input class="input-num" v-model="data.EvaluationValue" type="number" />
			<div>比例（%）通过，按总分计算百分比统计评估结果</div>
		</div>

		<div class="bottom_submit">
			<eagle-button type="info" @click="_submit()"> 确定 </eagle-button>
		</div>
		<!-- 选择试卷 -->
		<ExamListDialog title="选择试卷" :selectedValue="data.PaperCode" :show.sync="examListShow" @callback="_chooseExamList" ></ExamListDialog>
		<!-- 选择人 -->
		<UserChoose
			title="选择人员"
			:show.sync="UserChooseShow"
			:selectedValue="data.MarkUserName"
			@callback="_chooseUserCb"
		></UserChoose>
	</div>
</template>

<script>
import { formatDate, getDateTime } from '@/utils/getDate'
import UserChoose from "@/views/components/user/userChooseDialog.vue";
import ExamListDialog from "@/views/components/train/examListDialog.vue";
import store from "@/store/index";
export default {
	components: {
		UserChoose,
		ExamListDialog
	},
	data() {
		return {
			data: {MarkChnName:"",MarkUserName:""},
			type: '',
			currentDate: new Date(),
			show: false,
			examListShow: false,
			UserChooseShow: false,
			minDate: new Date(2020, 0, 1),
			maxDate: new Date(2030, 12, 30),
		}
	},
	created() {},
	activated() {
		// 数据

		if (this.$route.params.data) {
			this.data = this.$route.params.data
			if (this.$route.params.data.ExamStartTime) {
				this.data.ExamStartTime = getDateTime(
					this.$route.params.data.ExamStartTime
				)
			}
			if (this.$route.params.data.ExamEndTime) {
				this.data.ExamEndTime = getDateTime(this.$route.params.data.ExamEndTime)
			}
		}
		// 试卷
		if (this.$route.params.examData) {
			this.data.PaperCode = this.$route.params.examData.PaperCode
			this.data.PaperName = this.$route.params.examData.PaperName
			//    this.$set(this.data, "PaperName", this.$route.params.examData.PaperName);
		}
		if (this.$route.params.type) {
			const type = this.$route.params.type
			// 阅卷人
			if (type == 'MarkChnName') {
				var MarkUserName = this.$route.params.userlist.map((item) => {
					return item.code
				})
				var MarkChnName = this.$route.params.userlist.map((item) => {
					return item.name
				})
				this.data.MarkUserName = MarkUserName.join(',')
				this.data.MarkChnName = MarkChnName.join(',')
			}
		}
	},
	methods: {
		// 显示弹窗
		showPopup(type, val) {
			if (val) {
				this.currentDate = new Date(val)
			} else {
				this.currentDate = new Date()
			}
			if (type == 'ExamStartTime') {
				this.type = 'ExamStartTime'
			} else {
				this.type = 'ExamEndTime'
			}
			this.show = true
		},
		// 确认选择的时间
		confirmPicker(val) {
			const dateTime = formatDate(val)
			if (this.type == 'ExamEndTime') {
				let beginTime = new Date(this.data.ExamStartTime)
				if (beginTime.getTime() > val.getTime()) {
					this.$toast('请选择当前日期之后的有效日期')
					this.show = false
					return
				} else {
					this.data[this.type] = getDateTime(dateTime)
					this.show = false
				}
			} else {
				this.data[this.type] = getDateTime(dateTime)
				this.show = false
			}
		},
		// 选择试卷
		chooseExam() {
			this.examListShow = true
			// this.$router.push({
			//   name: "ExamList",
			//   params: {
			//     path: "setPlanExam",
			//   },
			// });
		},

		_chooseUserCb(item) {
			
			// 阅卷人
			console.log(item, 'item')
			this.data.MarkUserName = item.UserName || '';
			this.data.MarkChnName = item.ChnName || '';
		},

		_chooseExamList(param) {
			if (param) {
				this.$set(this.data, 'PaperName', param.PaperName)
				this.data.PaperCode = param.PaperCode
				// this.data.PaperName = param.PaperName;
			}
			this.examListShow = false

			console.log('adasdasd', param)
		},

		// 选择人员
		goExamPeople(type) {
			this.UserChooseShow = true
			// this.$router.push({
			//   name: "ExamPeople",
			//   params: {
			//     type: type,
			//     path: "setPlanExam",
			//   },
			// });
		},
		// 保存
		_submit(IsPublish) {
			if (!this.data.PaperCode) {
				return this.$toast('请选择试卷')
			}
			if (!this.data.MarkUserName) {
				return this.$toast('请选择阅卷人')
			}
			if (!this.data.ExamStartTime) {
				return this.$toast('请选择开始时间')
			}
			if (!this.data.ExamEndTime) {
				return this.$toast('请选择结束时间')
			}
			if (!this.data.ExamMins) {
				return this.$toast('请输入考试时间')
			}
			if (!this.data.EvaluationType) {
				return this.$toast('请选择评估方式')
			}
			store.commit('updataSetPlanExamData', this.data)
			this.$router.go(-1)
			// this.$router.push({
			//   name: "AddTrainingPlan",
			//   params: {
			//     item: this.data,
			//   },
			// });
		},
	},
}
</script>

<style lang="less" scoped>
.add-exam {
	background-color: #e8f1f8;
	min-height: 100vh;
	padding-bottom: 30px;
	.bottom_submit {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		padding: 15px;
		box-sizing: border-box;
		.van-button {
			width: 100%;
			height: 35px;
		}
	}
}
.col-detail {
	color: #333;
	display: flex;
	align-items: center;
	justify-content: space-between;

	.col-del {
		display: flex;
		align-items: center;
		img {
			width: 15px;
			height: 15px;
			margin-right: 5px;
		}
	}
}
.valueclass {
	color: rgb(0, 195, 224);
}
.input-num {
	display: block;
	width: 50px;
	min-height: 20px;
	background-color: #e8f1f8;
	border: none;
	text-align: center;
}
.bottom-desc {
	min-height: 40px;
	display: flex;
	padding: 10px 15px;
	font-size: 13px;
	background-color: #fff;
	color: #646566;
	.input-num {
		margin: 0 5px;
	}
	span {
		width: 30px;
	}
}
</style>
